/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nostra13.sample.slice;

import com.nostra13.sample.ResourceTable;
import com.nostra13.sample.utils.FileIOUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ProgressBar;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class DetailAbilitySlice extends AbilitySlice {

    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_detail);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(ResourceTable.Media_ic_stub)
                .showImageForEmptyUri(ResourceTable.Media_ic_empty)
                .showImageOnFail(ResourceTable.Media_ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(PixelFormat.RGB_565)
                .build();

        Image image = (Image) findComponentById(ResourceTable.Id_image);
        ProgressBar progress = (ProgressBar) findComponentById(ResourceTable.Id_progress);

        String url = intent.getStringParam("url");
        if(FileIOUtils.isSpace(url)){
            ToastDialog toastDialog = new ToastDialog(this);
            toastDialog.setText("文件内容为空！").show();
        }else {

//            image.setPixelMap(ImageLoader.getInstance().loadImageSync(url));

//            ImageLoader.getInstance().loadImage(url, options, new SimpleImageLoadingListener() {
//                @Override
//                public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
//                    // Do whatever you want with PixelMap
//                    image.setPixelMap(loadedImage);
//                }
//            });

            ImageLoader.getInstance().displayImage(url, image, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, Component view) {
                    progress.setProgressValue(0);
                    progress.setVisibility(Component.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, Component view, FailReason failReason) {
                    progress.setVisibility(Component.HIDE);
                }

                @Override
                public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
                    progress.setVisibility(Component.HIDE);
                }
            }, new ImageLoadingProgressListener() {
                @Override
                public void onProgressUpdate(String imageUri, Component view, int current, int total) {
                    progress.setProgressValue(Math.round(100.0f * current / total));
                }
            });
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
            if (loadedImage != null) {
                Image imageView = (Image) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}
